﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using IOPath = System.IO.Path;
namespace g1_WPF
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            LoadDirectories();
        }

        private void LoadDirectories()
        {
            DirectoryInfo dirInfo = new DirectoryInfo(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile));
            foreach (DirectoryInfo dir in dirInfo.GetDirectories())
            {
                TreeViewItem item = new TreeViewItem()
                {
                    Header = dir.Name,
                    Tag = dir.FullName
                };
                item.Items.Add(null); // Placeholder for lazy loading
                item.Expanded += Folder_Expanded;
                FoldersTreeView.Items.Add(item);
            }
        }

        private void Folder_Expanded(object sender, RoutedEventArgs e)
        {
            TreeViewItem item = (TreeViewItem)sender;
            if (item.Items.Count == 1 && item.Items[0] == null)
            {
                item.Items.Clear();
                DirectoryInfo dirInfo = new DirectoryInfo((string)item.Tag);
                try
                {
                    foreach (DirectoryInfo subDir in dirInfo.GetDirectories())
                    {
                        TreeViewItem subItem = new TreeViewItem()
                        {
                            Header = subDir.Name,
                            Tag = subDir.FullName
                        };
                        subItem.Items.Add(null);
                        subItem.Expanded += Folder_Expanded;
                        item.Items.Add(subItem);
                    }
                }
                catch { }
            }
        }

        private void FoldersTreeView_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
        {
            TreeViewItem item = (TreeViewItem)FoldersTreeView.SelectedItem;
            FilesListView.Items.Clear();
            DirectoryInfo dirInfo = new DirectoryInfo((string)item.Tag);
            try
            {
                foreach (DirectoryInfo dir in dirInfo.GetDirectories())
                {
                    FilesListView.Items.Add(new { Name = dir.Name, Type = "Folder" });
                }
                foreach (FileInfo file in dirInfo.GetFiles())
                {
                    FilesListView.Items.Add(new { Name = file.Name, Type = file.Extension });
                }
            }
            catch { }
        }

        private void FilesListView_MouseDoubleClick(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            dynamic selectedItem = FilesListView.SelectedItem;
            if (selectedItem != null && selectedItem.Type == ".txt")
            {
                string path = IOPath.Combine(((TreeViewItem)FoldersTreeView.SelectedItem).Tag.ToString(), selectedItem.Name);

                
                System.Diagnostics.Process.Start("notepad.exe", path);
            }
        }
    }
}
